<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');
/**
 * Description of location
 *
 * @author b4by
 */
class location extends MY_Controller {
	function __construct() {
		parent::__construct();
		$this->load->model('model_location');
	}
	
	public function index()
	{
		$this->data['temp'] = 'admin/location/index';
		$this->load->view('admin/main',$this->data);
	}
	
	public function read()
	{
		
		$locations = $this->model_location->get_list_hash(array(
			'order'	=> array('index','asc')
		));
		
		array_to_object($locations);
		object_prefix_create($locations,'', '&nbsp;&nbsp;&nbsp;&nbsp;');

		$data = array();
		object_to_array($data, $locations);
		$data[0]->pre='';
		
		echo json_encode(array(
			'state'	=> 'success',
			'data'	=> $data,
		));
	}
	
	public function detail()
	{
		$id = request('id');
		$data = $this->model_location->get_info($id);
		$locations = $this->model_location->get_list_hash(array(
			'order'	=> array('index','asc')
		));
		array_to_object($locations);
		if($data!==false){
			delete_children($data, $locations);
			//pr($locations);die();
			object_prefix_create($locations, '', '&nbsp;&nbsp;&nbsp;&nbsp;');
			$data->parents = array();
			object_to_array($data->parents, $locations);
			echo json_encode(array(
				'state'	=> 'success',
				'data' => $data
			));
		}
		else
		{
			echo json_encode(array(
				'state'	=> 'error',
				'title'	=> lang('error'),
				'message'	=> lang('location_not_found')
			));
		}
		
	}
	public function create()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('name','lang:location_name','required');
		$data = request_filter(array('name','description','p_id'));
		if($this->form_validation->run()){
			$this->model_location->create($data);
			echo json_encode(array(
				'state'	=> 'success',
				'title'	=> lang('notice'),
				'messge'	=> lang('location_create_success'),
			));
		}
		else
		{
			echo json_encode(array(
				'state'	=> 'error',
				'title'	=> lang('error'),
				'message'	=> form_error_message($data)
			));
		}
	}
	
	public function update()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('name','lang:location_name','required');
		$data = request_filter(array('name','description','p_id','index'));
		if($this->form_validation->run()){
			$id = request('id');
			$this->model_location->update($id, $data);
			echo json_encode(array(
				'state'	=> 'success',
				'title'	=> lang('notice'),
				'id'	=> $id,
				'message'	=> lang('location_update_success'),
			));
		}
		else
		{
			echo json_encode(array(
				'state'	=> 'error',
				'title'	=> lang('error'),
				'message'	=> form_error_message($data)
			));
		}
	}
	public function delete()
	{
		$id = request('id');
		$children = $this->model_location->get_list(array(
			'where'	=> array('p_id'	=> $id)
		));
		if(count($children)==0){
			$this->model_location->del($id);
			echo json_encode(array(
				'state'	=> 'success',
				'title'	=> lang('notice'),
				'id'	=> $id,
				'message'	=> lang('location_delete_success')
			));
		}
		else
		{
			echo json_encode(array(
				'state'	=> 'error',
				'title'	=> lang('error'),
				'message'	=> lang('location_contain_child')
			));
		}
	}
	
	public function get_all(){
		echo json_encode(array(
			'state'	=> 'success',
			'data'	=> $this->model_location->get_list(array(
				'select'	=> 'id,name,p_id'
			)),
		));
	}
}

?>
